//
//  PlayView.m
//  播放按钮和暂停按钮自动转换
//
//  Created by dllo on 16/8/19.
//  Copyright © 2016年 yh. All rights reserved.
//

#import "PlayView.h"

@interface PlayView ()
@property (nonatomic, strong) UIButton *playButton;
@property (nonatomic, copy) void (^play)(UIButton *play);
@property (nonatomic, copy) void (^pause)(UIButton *pause);

@property (nonatomic, copy) void (^next)(UIButton *next);
@property (nonatomic, copy) void (^last)(UIButton *last);

@property (nonatomic, strong) UIButton *nextButton;
@property (nonatomic, strong) UIButton *lastButton;
@end


@implementation PlayView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setPlayButton];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self setPlayButton];
    }
    return self;
}

- (void)setPlayButton {
    self.playButton = [self setButtonWithImage:@"hp_player_btn_play_normal" action:@selector(play:)];
    self.nextButton = [self setButtonWithImage:@"hp_player_btn_next_normal" action:@selector(nextClick:)];
    self.lastButton = [self setButtonWithImage:@"hp_player_btn_pre_normal" action:@selector(lastClick:)];
}
- (void)lastClick:(UIButton *)button {
    self.last(button);
}
- (void)nextClick:(UIButton *)button {
    self.next(button);
}
// 点击播放按钮 触发方法
- (void)play:(UIButton *)playButton {
    [self.playButton setImage:[UIImage imageNamed:@"hp_player_btn_pause_normal"] forState:UIControlStateNormal];
    [self.playButton removeTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
    [self.playButton addTarget:self action:@selector(pause:) forControlEvents:UIControlEventTouchUpInside];
    self.play(playButton);
}
// 点击暂停按钮触发方法
- (void)pause:(UIButton *)pauseButton {
    [self.playButton setImage:[UIImage imageNamed:@"hp_player_btn_play_normal"] forState:UIControlStateNormal];
     [self.playButton removeTarget:self action:@selector(pause:) forControlEvents:UIControlEventTouchUpInside];
    [self.playButton addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
    self.pause(pauseButton);
}
// 因为重复代码太多 写一个返回button的方法
- (UIButton *)setButtonWithImage:(NSString *)imageName action:(SEL)action {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button];
    return button;
}
// 实现block方法
- (void)play:(void (^)(UIButton *))play pause:(void (^)(UIButton *))pause {
    self.play = play;
    self.pause = pause;
}

- (void)next:(void (^)(UIButton *))next last:(void (^)(UIButton *))last {
    self.next = next;
    self.last = last;
}


- (void)layoutSubviews {
    [super layoutSubviews];
    self.playButton.frame = CGRectMake(0, 0, 100, 100);
    self.playButton.center = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2);
    
    self.nextButton.frame = CGRectMake(0, 0, 75, 75);
    self.nextButton.center = CGPointMake(self.playButton.center.x  + self.playButton.bounds.size.width / 2 + 75 / 2, self.bounds.size.height / 2);
    
    self.lastButton.frame = CGRectMake(0, 0, 75, 75);
    self.lastButton.center = CGPointMake(self.playButton.center.x  - self.playButton.bounds.size.width / 2 - 75 / 2, self.bounds.size.height / 2);
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
